<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Api extends CI_Controller {


    function __construct()
    {
        parent::__construct();
        $this->load->model('user_model', 'user');

    }

    public function _check_user()
    {
        $openid = $this->session->userdata('user');
        //$openid = $this->input->get('user');

        if (empty($openid)) {
            $this->_json('', FALSE, '不存在openid');
            // redirect(site_url('callback/authorize'));
            exit;
        }
        $user_info = $this->user->get_user_by_openid($openid);
        if (empty($user_info)) {
            // redirect(site_url('callback/authorize'));
            $this->_json('', FALSE, '不存在用户');
            exit;
        }
        return $user_info;

    }

    public function _json($data='', $status=TRUE, $err_msg='')
    {
        $result = array('data'=>$data, 'status'=>$status);
        if ($status !== TRUE) {
            $result['err_msg'] = $err_msg;
        }

        header('Content-Type: application/json');
        $callback = $this->input->get('cb');
        if (!empty($callback)) {
          echo $callback . '(' . json_encode($result) . ');';
        } else {
          echo json_encode($result);
        }
        exit;

    }

    public function index()
    {

    }

    public function user_info()
    {
        $user_info = $this->_check_user();
        $this->_json($user_info);
    }

    public function start_game()
    {
        $user_info = $this->_check_user();
        # 生命减一
        $life = $user_info->life;

        $life = $life - 1;
        $this->user->set_life($user_info->openid, $life);

        $this->load->model('code_model');
        if ($result = $this->code_model->check_user($user_info->uid)) {
            $this->_json(array('code'=>$result->code, 'status'=>TRUE, 'type'=>$result->type, 'life'=>$life+1));
        }
        $this->_json(array('life'=>$life+1, 'status'=>FALSE));
    }

    public function share_success()
    {
        $user_info = $this->_check_user();
        // if ($user_info->life > 2) {
        //     $this->_json('', FALSE, '');
        // }
        if ($user_info->life < 0) {
            $life = 0;
        } else {
            $life = $user_info->life+1;
        }
        $this->user->set_life($user_info->openid, $life);
        $user_info->life = $user_info->life+1;
        $this->_json($life+1);
    }

    public function end_game()
    {
        $user_info = $this->_check_user();
        $level = $this->input->post('level');
        $this->load->model('code_model');

        if ($level > $user_info->top_level) {
            $this->user->set_top_level($user_info->openid, $level);
            $user_info->top_level = $level;
        }

        $rank = $this->user->get_rank($level);
        $top_rank = $this->user->get_rank($user_info->top_level);

        $data = array('rank'=>$rank,
                      'top_rank'=>$top_rank,
                      'top_level'=>$user_info->top_level,
                      'level'=>$level,
                      'life'=>$user_info->life,
                      'need_info'=>FALSE);
        if (!$this->user->check_top_chance($user_info->uid) && $rank <= 3) {
            $data['need_info'] = TRUE;
        }

        $this->_json($data);
    }

    public function get_code()
    {
        $user_info = $this->_check_user();

        $level = $this->input->post('level');
        if ($level == null) {
            $this->_json('', FALSE, '');
        }
        $this->load->model('code_model');
        if ($result = $this->code_model->check_user($user_info->uid)) {
            $this->_json(array('code'=>$result->code, 'status'=>TRUE, 'type'=>$result->type));
        }
        $type = FALSE;
        if ($level >= NBA2) {
            $type = 2;
        } else if ($level >= NBA1) {
            $type = 1;
        } else if ($level >= NBA0) {
            $type = 0;
        }
        $code = NULL;

        if ($type !== FALSE) {
           $result = $this->code_model->get_by_uid($user_info->uid, $type);
            if ($result) {
                $code = $result->code;
            } else {
                $code = $this->code_model->allot_code($user_info->uid, $type);
            }
        }


        $this->_json(array('code'=>$code, 'type'=>$type, 'status'=>FALSE));

    }

    public function get_rank()
    {
        $user_info = $this->_check_user();

        $level = $this->input->get('level');
        if ($level == null) {
            $this->_json('', FALSE, '');
        }
        $rank = $this->user->get_rank($level);
        $this->_json($rank);
    }

    public function add_user_info()
    {
        $user_info = $this->_check_user();

        $rank = $this->user->get_rank($user_info->top_level);
        if ($rank > 4) {
            $this->_json('', FALSE, '您没有机会，请再接再厉');
        }
        $this->load->model('user_info_model');

        $_user = $this->user_info_model->get_by_uid($user_info->uid);
        if (empty($_user)) {
            $name = $this->input->post('name');
            $phone = $this->input->post('phone');
            $address = $this->input->post('address');

            $this->user_info_model->insert(array('name'=>$name, 'phone'=>$phone, 'address'=>$address),
                                           $user_info->uid);
            $this->_json(TRUE);
        } else {
            $this->_json(FALSE);
        }
    }
}
